//https://www.bilibili.com/video/BV1d54y1f7B3/?spm_id_from=333.337.search-card.all.click&vd_source=d6fe65f095f01f05591e89d810720ccc
// 2024-10-18


import java.util.Arrays;

public class P004_寻找数组的中心下标_双指针法 {

    public static void main(String[] args) {
        System.out.println(fun(new int[]{1, 7, 3, 6, 5, 6}));
    }

    // 给个数组nums,寻找中心下标，左侧元素相加=右侧元素相加想
    private static int fun(int[] nums) {
        int sum = Arrays.stream(nums).sum(); //JDK8流式求和
        int total = 0;

        for (int i = 0;i < nums.length;i++) {
            sum = sum - nums[i];//总和指针递减
            if (total == sum) return i;
            total += nums[i];//左和指针递增
        }

        return -1;
    }


}
